package com.zw.mes.module.proStrucEdit.service;

import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.sys.User;
import com.zw.mes.module.cost.model.Project;
import com.zw.mes.module.proStrucEdit.mapper.ProStrucEditMapper;
import com.zw.mes.module.proStrucEdit.model.ProStrucEditProjectModel;
import com.zw.mes.module.proStrucEdit.model.SpecialModel;
import com.zw.mes.module.proStrucEdit.model.VwDsnProStrucModel;
import com.zw.mes.module.prodtaskmanager.entity.VwProject;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 徐一贺
 * @className ProStrucEditService
 * @description 产品结构编辑业务逻辑层
 * @date 2021/3/11 15:14
 * @versron ver 1.0
 */
@Log4j2
@Service
public class ProStrucEditService {
    private LogService logService;

    @Autowired
    private void setLogService(LogService logService) {
        this.logService = logService;
    }

    @Autowired
    private ProStrucEditMapper mapper;

    /**
     * 加载可编辑非设计类项目列表
     *
     * @param param
     * @return
     */
    public List<ProStrucEditProjectModel> findProStrucEditProjectList(ProStrucEditProjectModel param) {
        return mapper.ProStrucEditProjectModel(param);
    }

    /**
     * 获取专业列表
     *
     * @return
     */
    public List<SpecialModel> findSpecialList() {
        return mapper.findSpecialList();
    }

    /**
     * 根据项目视图主键获取项目信息
     *
     * @param proId 项目视图主键
     * @return
     */
    public VwProject getVwProjectByPrimaryKey(Integer proId) {
        return mapper.getVwProjectByPrimaryKey(proId);
    }

    /**
     * 加载可编辑非设计类项目的产品结构列表
     *
     * @param param
     * @return
     */
    public List<VwDsnProStrucModel> findProStrucEditList(VwDsnProStrucModel param) {
        return mapper.findProStrucEditList(param);
    }

    /**
     * 提交
     *
     * @param idStr
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult submit(String idStr) {
        JsonResult jsonResult = new JsonResult();
        try {
            List<VwDsnProStrucModel> vwDsnProStrucModelList = mapper.findVwDsnProStrucModelByPrimaryKeys(idStr);
            for (int i = 0; i < vwDsnProStrucModelList.size(); i++) {
                VwDsnProStrucModel vwDsnProStrucModel = vwDsnProStrucModelList.get(i);
                VwProject project = mapper.getVwProjectByPrimaryKey(vwDsnProStrucModel.getProId());
                vwDsnProStrucModel.setStatus(3);
                vwDsnProStrucModel.setEndTime(new Date());
                int result = mapper.updateVwDsnProStrucModelCompStatusByPrimaryKey(vwDsnProStrucModel);
                if (result > 0) {
                    logService.saveLog("产品结构编辑", "项目编号为：【" + project.getProCode() + "】，项目名称为：【" + project.getProName() + "】的项目的名称为【" + vwDsnProStrucModel.getProPart() + "】,专业为【" + vwDsnProStrucModel.getSpecial() + "】的产品结构被提交了。");
                }
            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("产品结构提交异常：" + e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("提交失败，请联系管理员！");
        }
        return jsonResult;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult remove(Integer id) {
        JsonResult jsonResult = new JsonResult();
        try {
            VwDsnProStrucModel vwDsnProStrucModel = mapper.getVwDsnProStrucModelByPrimaryKey(id);
            VwProject project = mapper.getVwProjectByPrimaryKey(vwDsnProStrucModel.getProId());
            vwDsnProStrucModel.setIsDel(1);
            int result = mapper.updateVwDsnProStrucModelIsDelByPrimaryKey(vwDsnProStrucModel);
            if (result > 0) {
                logService.saveLog("产品结构编辑", "项目编号为：【" + project.getProCode() + "】，项目名称为：【" + project.getProName() + "】的项目的名称为【" + vwDsnProStrucModel.getProPart() + "】,专业为【" + vwDsnProStrucModel.getSpecial() + "】的产品结构被删除了。");
            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("产品结构删除异常：" + e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("删除失败，请联系管理员！");
        }
        return jsonResult;
    }

    /**
     * 保存
     *
     * @param vwDsnProStrucModel
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult save(VwDsnProStrucModel vwDsnProStrucModel) {
        JsonResult jsonResult = new JsonResult();
        try {
            Date currentTime = new Date();
            User user = ShiroUtils.getUser();
            if(proPartExist(vwDsnProStrucModel)==false){
                jsonResult.setType(JsonResult.ResultType.ERROR);
                jsonResult.setMsg("产品结构已存在或该专业已有整体产品结构，请重新命名本产品结构名称或修改整体的产品结构名称！");
            }else {
                if (vwDsnProStrucModel.getId() == null) {
                    vwDsnProStrucModel.setCreateCode(user.getAccount());
                    vwDsnProStrucModel.setCreateName(user.getName());
                    vwDsnProStrucModel.setCreateTime(currentTime);
                    vwDsnProStrucModel.setStartTime(currentTime);
                    vwDsnProStrucModel.setCompStatus(0);
                    vwDsnProStrucModel.setStatus(0);
                    vwDsnProStrucModel.setRejectReason("");
                    vwDsnProStrucModel.setIsDel(0);
                    vwDsnProStrucModel.setDeptId(user.getDeptId());
                    int result = mapper.insertvwDsnProStrucModel(vwDsnProStrucModel);
                    VwProject project = mapper.getVwProjectByPrimaryKey(vwDsnProStrucModel.getProId());
                    if (result > 0) {
                        logService.saveLog("产品结构编辑-添加产品结构", "项目编号为：【" + project.getProCode() + "】，项目名称为：【" + project.getProName() + "】的项目，新增了名称为【" + vwDsnProStrucModel.getProPart() + "】,专业为【" + vwDsnProStrucModel.getSpecial() + "】的产品结构。");
                    }
                }
                if (vwDsnProStrucModel.getId() != null) {
                    vwDsnProStrucModel.setStatus(0);
                    vwDsnProStrucModel.setCompStatus(0);
                    vwDsnProStrucModel.setUpdateName(user.getName());
                    vwDsnProStrucModel.setUpdateTime(currentTime);
                    VwDsnProStrucModel before = mapper.getVwDsnProStrucModelByPrimaryKey(vwDsnProStrucModel.getId());
                    int result = mapper.updateDsnProStrucModelToEdit(vwDsnProStrucModel);
                    VwProject project = mapper.getVwProjectByPrimaryKey(vwDsnProStrucModel.getProId());
                    if (result > 0) {
                        logService.saveLog("产品结构编辑-编辑产品结构", "项目编号为：【" + project.getProCode() + "】，项目名称为：【" + project.getProName() + "】的项目，编辑产品结构，编辑前产品结构名称为【" + before.getProPart() + "】,专业为【" + before.getSpecial() + "】,编辑后产品结构名称为【" + vwDsnProStrucModel.getProPart() + "】,专业为【" + vwDsnProStrucModel.getSpecial() + "】。");
                    }
                }
                jsonResult.setType(JsonResult.ResultType.SUCCESS);
                jsonResult.setMsg("保存成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存产品结构异常：" + e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败，请联系管理员！");
        }
        return jsonResult;
    }

    /**
     * 部分验证
     *
     * @param vwDsnProStrucModel
     * @return
     */
    public Boolean proPartExist(VwDsnProStrucModel vwDsnProStrucModel) {
        Boolean boolExist = false;
        if(vwDsnProStrucModel.getId()!=null){
            if("整体".equals(vwDsnProStrucModel.getProPart())){
                boolExist = true;
            }else{
                VwDsnProStrucModel wholeVwDsnProStrucExistModel = mapper.getWoleVwDsnProStrucExistModelByProIdAndNotId(vwDsnProStrucModel);
                if(wholeVwDsnProStrucExistModel!=null){
                    return false;
                }else {
                    VwDsnProStrucModel vwDsnProStrucExistModel = mapper.getVwDsnProStrucModelByProIdAndProPart(vwDsnProStrucModel);
                    if (vwDsnProStrucExistModel == null) {
                        boolExist = true;
                    } else {
                        boolExist = false;
                    }
                }
            }
        }else{
            VwDsnProStrucModel wholeVwDsnProStrucExistModel = mapper.getWoleVwDsnProStrucExistModelByProId(vwDsnProStrucModel);
            if (wholeVwDsnProStrucExistModel == null) {
                if("整体".equals(vwDsnProStrucModel.getProPart())){
                    boolExist = true;
                }else{
                    VwDsnProStrucModel vwDsnProStrucExistModel = mapper.getVwDsnProStrucModelByProIdAndProPart(vwDsnProStrucModel);
                    if (vwDsnProStrucExistModel == null) {
                        boolExist = true;
                    } else {
                        boolExist = false;
                    }
                }
            } else {
                boolExist = false;
            }
        }
        return boolExist;
    }
}
